package guestbook;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StoreEvent extends HttpServlet {
    private static final Logger log =
            Logger.getLogger(StoreEvent.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
    	
    		try{
		        UserService userService = UserServiceFactory.getUserService();
		        User user = userService.getCurrentUser();
		
		        // We have one entity group per Guestbook with all Greetings residing
		        // in the same entity group as the Guestbook to which they belong.
		        // This lets us run an ancestor query to retrieve all Greetings for a
		        // given Guestbook. However, the write rate to each Guestbook should be
		        // limited to ~1/second.
		        String CalendarName = "IIITD-Calendar";
		        Key guestbookKey = KeyFactory.createKey("Calendar", CalendarName);
		        String Event_Name = req.getParameter("Event_Name");
		        String Event_Date = req.getParameter("Event_Date");
		        String Event_Location = req.getParameter("Event_Location");
		        String Tags = req.getParameter("Event_Tags");
		        String Time = req.getParameter("Event_Time");
		        String [] Times  = Time.split(":");
		        String Event_Description = req.getParameter("Event_Description");
		        //System.out.println(Tags);
		        //System.out.println(Tags.split(",").toString());
		        String [] Tags_List = Tags.split(",");
		        String [] Date_List = Event_Date.split("/");
		        Date CreatedOn = new Date();
		        Date EventDate = new Date();
		        EventDate.setHours(Integer.parseInt(Times[0]));
		        EventDate.setMinutes(Integer.parseInt(Times[1]));
		        EventDate.setDate(Integer.parseInt(Date_List[0]));
		        EventDate.setMonth(Integer.parseInt(Date_List[1]));
		        EventDate.setYear(Integer.parseInt(Date_List[2]));
		        
		        //date.set
		        Entity Event = new Entity("Event", guestbookKey);
		        Event.setProperty("CreatedBy", user);
		        Event.setProperty("CreatedOn", CreatedOn);
		        Event.setProperty("Event_Name", Event_Name);
		        Event.setProperty("Event_Location", Event_Location);
		        Event.setProperty("Tags_list", Tags);
		        Event.setProperty("Event_Date", EventDate.toGMTString());
		        
		
		        DatastoreService datastore =
		                DatastoreServiceFactory.getDatastoreService();
		        datastore.put(Event);
		
		        resp.sendRedirect("/Event-Creation.jsp?CreationResult=Success" );
    		}
    		catch(Exception E)
    		{
    			
    			//System.out.println("Failed due to " + E.toString()); 
    			resp.sendRedirect("/Event-Creation.jsp?CreationResult=Failure" );
    		}
    }
}
